package com.example.weathercache.controller;

import com.example.citycommon.dto.WeatherResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cache")
public class CacheController {
    @Autowired
    private RedisTemplate<String, WeatherResponse> redisTemplate;

    @GetMapping
    public WeatherResponse getWeatherFromCache(@RequestParam("cityid") String cityId) {
        return (WeatherResponse) redisTemplate.opsForHash().get("weatherdata", cityId);
    }

    @PostMapping
    public void saveWeatherToCache(@RequestParam("cityid") String cityId, @RequestBody WeatherResponse weatherResponse) {
        redisTemplate.opsForHash().put("weatherdata", cityId, weatherResponse);
    }
}
